﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ_Common;

namespace RabbitMQ_COM.Exchange
{
    /// <summary>
    /// 扇形匹配
    /// </summary>
    public class FanoutService
    {
        public static void Consumption()
        {

            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())//创建信道，通信管道
                {
                    channel.ExchangeDeclare("fanout_exchange", "fanout");
                    //创建队列
                    //创建队列
                    var queue = "fanout1";
                    channel.QueueDeclare(queue, false, false, false, null);
                    var queue2 = "fanout2";
                    channel.QueueDeclare(queue2, false, false, false, null);
                    var queue3 = "fanout3";
                    channel.QueueDeclare(queue3, false, false, false, null);
                    //绑定交换机
                    channel.QueueBind(queue, "fanout_exchange", "");
                    channel.QueueBind(queue2, "fanout_exchange", "");
                    channel.QueueBind(queue3, "fanout_exchange", "");
                    while (true)
                    {
                        //没绑定交换机，但由于rabbitmq有默认交换机
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var message = Encoding.UTF8.GetString(ea.Body.ToArray());
                            Console.WriteLine(message);

                        };
                        channel.BasicConsume(queue, true, consumer);
                        Thread.Sleep(1000);
                    }


                }
            }
        }
    }
}
